//
//  OrderDetailheader.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/2.
//  Copyright © 2018年 四川中农. All rights reserved.
//



import UIKit

class OrderDetailheader: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    var model:OrderListModel!
    
    var orderNum:UILabel!
    
    var orderTime:UILabel!
    
    var timeDown:UILabel!
    
    var statusLabel:UILabel!
    
    var timer:Timer!

    init(frame:CGRect,model:OrderListModel) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.backgroundColor
        self.model = model
        self.initContent(status: model.state)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func initContent(status:Int){
        var maxHeight:CGFloat = 0
        if status == OrderStatus.waitPay.rawValue {
            let bgView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: 100))
            bgView.backgroundColor = UIColor.nav_color
            self.addSubview(bgView)
            
            
            var text = ""
            var imageName = "dingdan_dzf"
            if self.model.type == 1 {
                text = "货物已确认送达，请确认后支付货款完成收货"
                imageName = "dingdan_hdfk"
            }
            
            let imageV = UIImageView.init(image: UIImage.init(named: imageName))
            imageV.frame = CGRect.init(x: 0, y: 23.5, width: 30, height: 30)
            imageV.centerX = bgView.centerX
            bgView.addSubview(imageV)
            
            
            timeDown = Utils.labelWith(frame: CGRect.init(x: 0, y: imageV.bottom + 9.5, width: bgView.width, height: 13), font: UIFont.systemFont(ofSize: 14), textAlignment: .center, textColor: "#333333", text: text)
            bgView.addSubview(timeDown)
            maxHeight = bgView.bottom
            
        }
        
        var view = UIView.init(frame: CGRect.init(x: 0, y: maxHeight + 10, width: UIScreen.ScreenWidth, height: 55))
        view.backgroundColor = UIColor.nav_color
        self.addSubview(view)
        
        self.orderNum = Utils.labelWith(frame: CGRect.init(x: 13, y: 10.5, width: UIScreen.ScreenWidth - 22 - 80, height: 13.5), font: UIFont.boldSystemFont(ofSize: 14), textAlignment: .left, textColor: "#000000", text: "订单号：" + model.number)
        view.addSubview(self.orderNum)
        
        let timeImage = UIImageView.init(image: UIImage.init(named: "time_2"))
        timeImage.frame = CGRect.init(x: self.orderNum.left, y: self.orderNum.bottom + 10, width: 12, height: 12)
        view.addSubview(timeImage)
        
        self.orderTime = Utils.labelWith(frame: CGRect.init(x: timeImage.right + 5, y: 0, width: UIScreen.ScreenWidth - timeImage.right + 5 - 80, height: 9.5), font: UIFont.systemFont(ofSize: 12), textAlignment: .left, textColor: "#999999", text: "\(model.createTime)".timeStampToString(timeFormat: "YYYY-MM-dd HH:mm:ss"))
        self.orderTime.centerY = timeImage.centerY - 0.5
        view.addSubview(self.orderTime)
        
        var statusTitle = ""
        switch status {
        case 0:
            statusTitle = "待支付"
        case 2:
            statusTitle = "待收货"
        case 3:
            statusTitle = "已完成"
        case 4:
            statusTitle = "已取消"
        default:
            break
        }
        
        self.statusLabel = Utils.labelWith(frame: CGRect.init(x: UIScreen.ScreenWidth - 12 - 80, y: 0, width: 80, height: 12.5), font: UIFont.systemFont(ofSize: 13), textAlignment: NSTextAlignment.right, textColor: "#f7c201", text: statusTitle)
        self.statusLabel.centerY = view.bounds.size.height/2
        view.addSubview(self.statusLabel)
        maxHeight = view.bottom
        
        view = UIView.init(frame: CGRect.init(x: 0, y: maxHeight, width: UIScreen.ScreenWidth, height: 10))
        view.backgroundColor = UIColor.backgroundColor
        self.addSubview(view)
        maxHeight = view.bottom
        self.height = maxHeight
        
        if model.state == 0 && model.type == 0 {
            self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateOrderStatus), userInfo: nil, repeats: true)
            RunLoop.current.add(self.timer, forMode: .commonModes)
        }
    }
    
    @objc func updateOrderStatus(){
        let countDown = model.endTime - (Date().timeIntervalSince1970)*1000
        let value = Utils.getMMSSFromSS(count: Int(countDown)/1000)
        var text = ""
        if value.0 != "00" {
            text = value.0 + "时"
        }
        if value.1 != "00" {
            text = text + value.1 + "分"
        }
        if value.2 != "00" {
            text = text + value.1 + "秒"
        }
        self.timeDown.text = "请在" + text + "内完成支付"
    }
    
    
}







